package com.xinchuang.service.targetSystem.Impl.damage;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinchuang.comment.R;
import com.xinchuang.entity.targetSystem.damage.TMissileGuide;
import com.xinchuang.entity.targetSystem.damage.TWarhead;
import com.xinchuang.manager.TMissileGuideManager;
import com.xinchuang.mapper.targetSystem.damage.TMissileGuideMapper;
import com.xinchuang.mapper.targetSystem.damage.TWarheadMapper;
import com.xinchuang.service.targetSystem.damage.TMissileGuideService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 制导方式
 */
@Service
public class TMissileGuideServiceImpl implements TMissileGuideService {

    @Autowired
    private TMissileGuideMapper tMissileGuideMapper;

    @Autowired
    private TMissileGuideManager tMissileGuideManager;

    @Autowired
    private TWarheadMapper tWarheadMapper;

    @Override
    public R addAndUpdateMissileGuide(TMissileGuide tMissileGuide) {
        int num = 0;
        if (StringUtils.isEmpty(tMissileGuide.getId())) {
            String tMissileGuideId = IdUtil.fastSimpleUUID();
            tMissileGuide.setId(tMissileGuideId);
            tMissileGuide.setDataStatus(0);
            num = tMissileGuideMapper.insert(tMissileGuide);
        } else {
            num = tMissileGuideMapper.updateById(tMissileGuide);
        }
        if (num > 0) {
            return R.success("操作成功");
        } else {
            return R.fail("操作失败");
        }
    }

    @Override
    public void deleteMissileGuide(String id) {
        tMissileGuideMapper.deleteById(id);
    }

    @Override
    public void deleteWarheadGuide(String id, boolean deleteStatus) {
        // 物理删除
        if (deleteStatus) {
            tWarheadMapper.deleteById(id);
            // 逻辑删除
        } else {
            tWarheadMapper.update(null, new LambdaUpdateWrapper<TWarhead>()
                    .set(TWarhead::getDataStatus, 1)
                    .eq(TWarhead::getId, id)
            );
        }
    }

    @Override
    public Map<String, Object> listMissileGuide(Integer pageSize, Integer pageNum, String missileId, String guideMeans, String guideType) {
        // 分页信息
        Page<TMissileGuide> page = new Page<>(pageNum, pageSize);
        Page<TMissileGuide> tMissileGuidePage = tMissileGuideMapper.selectPage(page, new LambdaQueryWrapper<TMissileGuide>()
                .like(StringUtils.isNotEmpty(guideMeans), TMissileGuide::getGuideMeans, guideMeans)
                .eq(StringUtils.isNotEmpty(missileId), TMissileGuide::getMissileId, missileId)
                .eq(StringUtils.isNotEmpty(guideType), TMissileGuide::getGuideType, guideType)
                // 未被逻辑删除
                .eq(TMissileGuide::getDataStatus, 0)
        );
        //重写定义分页插件返回参数
        Map<String, Object> map = new HashMap<>();
        map.put("dataList", tMissileGuidePage.getRecords());
        map.put("total", tMissileGuidePage.getTotal());
        return map;
    }
}
